package views;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import controllers.updateControllers.UpdateMaatschappijController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.UpdateMaatschappijModel;
import core.models.domainModel.Adres;
import core.models.domainModel.Maatschappij;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateMaatschappijView extends JFrame{
	private static final long serialVersionUID = 1L;
	private JTextField txtNaam;
	private JTextField txtStraat;
	private JTextField txtHuisnr;
	private JTextField txtBus;
	private JTextField txtPostCode;
	private JTextField txtGemeente;
	private JTextField txtLand;
	private UpdateMaatschappijModel _model;
	private JTextField txtProvincie;
	
	public UpdateMaatschappijView(UpdateMaatschappijModel model) {
		setTitle("Update Maatschappij");
		
		_model = model;
		final Maatschappij maatschappij = model.getMaatschappij();
//		final Adres adres = model.getMaatschappij().getAdres();
		final Adres adres = maatschappij.getAdres();
		
		JLabel lblMaatschappijNaam = new JLabel("Maatschappijnaam: ");
		
		JLabel lblStraat = new JLabel("Straat: ");
		
		JLabel lblHuisnr = new JLabel("Huisnr.: ");
		
		JLabel lblBus = new JLabel("Bus: ");
		
		JLabel lblPostcode = new JLabel("Postcode: ");
		
		JLabel lblGemeente = new JLabel("Gemeente: ");
		
		JLabel lblLand = new JLabel("Land:");
		
		txtNaam = new JTextField();
		txtNaam.setColumns(10);
		txtNaam.setText(maatschappij.getNaam());
		
		txtStraat = new JTextField();
		txtStraat.setColumns(10);
		txtStraat.setText(adres.getStraat());
		
		txtHuisnr = new JTextField();
		txtHuisnr.setColumns(10);
		txtHuisnr.setText(adres.getHuisnummer().toString());
		
		txtBus = new JTextField();
		txtBus.setColumns(10);
		txtBus.setText(adres.getBus());
		
		txtPostCode = new JTextField();
		txtPostCode.setColumns(10);
		txtPostCode.setText(adres.getPostcode());
		
		txtGemeente = new JTextField();
		txtGemeente.setColumns(10);
		txtGemeente.setText(adres.getGemeente());
		
		txtLand = new JTextField();
		txtLand.setColumns(10);
		txtLand.setText(adres.getLand());
		
		JButton btnWijzigen = new JButton("Wijzigen");
		btnWijzigen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(validateView(UpdateMaatschappijView.this) == true)
				{
				adres.setStraat(txtStraat.getText());
				adres.setHuisnummer(Integer.parseInt(txtHuisnr.getText()));
				adres.setBus(txtBus.getText());
				adres.setGemeente(txtGemeente.getText());
				adres.setPostcode(txtPostCode.getText());
				adres.setStaatProvincie(txtProvincie.getText());
				adres.setLand(txtLand.getText());
				
				
				maatschappij.setNaam(txtNaam.getText());
				maatschappij.setAdres(adres);

				_model.setMaatschappij(maatschappij);
				
				UpdateMaatschappijController.handleView(_model);
				}
			}
		});
		
		JButton btnAnnuleren = new JButton("Annuleren");
		btnAnnuleren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UpdateMaatschappijController.returnToMaatschappijView();
			}
		});
		
		JLabel lblStaatprovincie = new JLabel("Staat/Provincie:  ");
		
		txtProvincie = new JTextField();
		txtProvincie.setText((String) null);
		txtProvincie.setColumns(10);
		txtProvincie.setText(adres.getStaatProvincie());
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(25)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblStraat)
								.addGap(18)
								.addComponent(txtStraat))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblMaatschappijNaam)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblGemeente)
									.addComponent(lblLand))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(txtLand, GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
									.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblHuisnr)
								.addGap(18)
								.addComponent(txtHuisnr, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
								.addGap(26)
								.addComponent(lblBus)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblStaatprovincie, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtProvincie, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblPostcode)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtPostCode, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnWijzigen)
							.addGap(26)
							.addComponent(btnAnnuleren, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(318, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMaatschappijNaam)
						.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblStraat)
						.addComponent(txtStraat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblHuisnr)
						.addComponent(txtHuisnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblBus)
						.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPostcode)
						.addComponent(txtPostCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblStaatprovincie)
						.addComponent(txtProvincie, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(22)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGemeente)
						.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLand)
						.addComponent(txtLand, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnWijzigen)
						.addComponent(btnAnnuleren))
					.addContainerGap(55, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		
	}

	private static Boolean validateView(UpdateMaatschappijView scherm)
	{
		if(!ValidatieService.isPresentAndLength(scherm.txtNaam, 150, "Naam"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtStraat, 150, "Straat"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtHuisnr, 20, "Huisnummer"))
			return false;
		if (!ValidatieService.isInteger(scherm.txtHuisnr, "Huisnummer"))
			return false;
		
		if(!ValidatieService.isLength(scherm.txtBus, 10, "Bus"))
			return false;
		
			
		
		
		if(!ValidatieService.isLength(scherm.txtGemeente, 150, "Gemeente"))
			return false;
		
		if(!ValidatieService.isLength(scherm.txtPostCode, 20, "Postcode"))
			return false;
	
			
		if(!ValidatieService.isLength(scherm.txtProvincie, 150, "Staat/Provincie"))
			return false;
		
		if(!ValidatieService.isLength(scherm.txtLand, 150, "Land"))
			return false;
		
		return true;
	}
	
}
